package stark.app.function.callshow;

import android.app.WallpaperManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.callshow.CallShowEntry;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.io.IOException;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import stark.app.R;
import stark.app.databinding.ActivityCallShowDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2021/5/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class CallShowDemoActivity extends BaseNoModelActivity<ActivityCallShowDemoBinding> {

    private static final int REQ_SET_LIVE_WALLPAPER = 1;

    @Override
    protected int onCreate() {
        return R.layout.activity_call_show_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSetCallShow.setOnClickListener(v -> {
            setCallShow();
        });
        mDataBinding.btnSetWallpaper.setOnClickListener(v -> {
            setWallpaper();
        });
        mDataBinding.btnSetLiveWallpaper.setOnClickListener(v -> {
            setLiveWallpaper(true);
        });
        mDataBinding.btnSetLiveWallpaperNoMute.setOnClickListener(v -> {
            setLiveWallpaper(false);
        });
        mDataBinding.btnClearWallpaper.setOnClickListener(v -> {
            clearMyWallpaper();
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_SET_LIVE_WALLPAPER) {
            ToastUtils.showShort(resultCode == RESULT_OK ? "设置动态壁纸成功" : "设置动态壁纸失败");
        }
    }

    private void setCallShow() {
        selVideo(new OnSelectResultListener<List<SelectMediaEntity>>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onResult(List<SelectMediaEntity> data) {
                Uri videoUri = Uri.parse(data.get(0).getUri());
                CallShowEntry.setCallShowVideo(videoUri, (success, msg) -> {
                    ToastUtils.showShort(msg);
                });
            }
        });
    }

    private void setWallpaper() {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @RequiresApi(api = Build.VERSION_CODES.M)
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        CallShowEntry.setWallpaper(data.get(0).getPath(), new CallShowEntry.SetWallpaperCallback() {
                            @Override
                            public void onStart() {
                                showDialog("设置壁纸中...");
                            }

                            @Override
                            public void onSetResult(boolean success) {
                                dismissDialog();
                                ToastUtils.showShort(success ? "壁纸设置成功" : "壁纸设置失败");
                            }
                        });
                    }
                });
    }

    private void setLiveWallpaper(boolean isMute) {
        selVideo(new OnSelectResultListener<List<SelectMediaEntity>>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onResult(List<SelectMediaEntity> data) {
                Uri videoUri = Uri.parse(data.get(0).getUri());
                if (isMute) {
                    //静音播放，可以调默认的静音播放接口
                    CallShowEntry.setVideoWallpaper(videoUri, CallShowDemoActivity.this, REQ_SET_LIVE_WALLPAPER);
                } else {
                    CallShowEntry.setVideoWallpaper(videoUri, CallShowDemoActivity.this, isMute, REQ_SET_LIVE_WALLPAPER);
                }
            }
        });
    }

    private void clearMyWallpaper() {
        try {
            WallpaperManager.getInstance(this).clear();
            ToastUtils.showShort("壁纸已清除");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void selVideo(OnSelectResultListener<List<SelectMediaEntity>> listener) {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.VIDEO)
                .maxCount(1)
                .listener(listener);
    }

    private interface Callback {


    }

}
